# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=oqsprovider
_pkgname=oqs-provider
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.8.0
pkgrel=1
pkgdesc="OpenSSL 3 provider containing post-quantum algorithms (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://openquantumsafe.org/applications/tls.html#oqs-openssl-provider'
msys2_repository_url="https://github.com/open-quantum-safe/oqs-provider"
license=('spdx:MIT')
depends=(
  "${MINGW_PACKAGE_PREFIX}-openssl"
  "${MINGW_PACKAGE_PREFIX}-liboqs"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
)
checkdepends=(
  "${MINGW_PACKAGE_PREFIX}-python-pytest"
)
source=(https://github.com/open-quantum-safe/$_pkgname/archive/$pkgver/$_realname-$pkgver.tar.gz
        "0001-fix-test-build.patch"
        "0002-fix-missing-prefix-relocation.patch")
sha256sums=('36d83229c360d694c1ce968f985375aa4e1d15b262ca4f8c354e53ea99fe9195'
            '73c44a80f10336a38f53bf7f08ad8c7ea49c63794c41023a81aba616763cb492'
            '4dd63f1a5aef651819951d085ef89a1e8458498b268b55b3e67632fcc0b0e040')

prepare() {
  cd "${_pkgname}-${pkgver}"

  patch -Np1 -i "${srcdir}/0001-fix-test-build.patch"
  patch -Np1 -i "${srcdir}/0002-fix-missing-prefix-relocation.patch"
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -S "${_pkgname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

check() {
  cmake --build "build-${MSYSTEM}" --target test
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}"/${_pkgname}-${pkgver}/LICENSE.txt \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt
}
